package com.breakfast.fun.view.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.ScreenUtil;

import java.util.List;

/**
 * @author sunsky
 * @version 创建时间：2016-6-1 下午4:07:31
 * @类说明 公告——文字上下轮播
 */
public class ScrollTextView extends LinearLayout {
    private TextView mBannerTV1;
    private TextView mBannerTV2;
    private Handler handler;
    private boolean isShow = false;
    private int startY1, endY1, startY2, endY2;
    private Runnable runnable;
    private List<String> list;
    private int position = 0;
    private int offsetY = 100;

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        offsetY = ScreenUtil.getScreenWidth(context) + 400;
        View view = LayoutInflater.from(context).inflate(
                R.layout.layout_home_scroll_text, this);
        mBannerTV1 = (TextView) view.findViewById(R.id.tv_home_scroll_text1);
        mBannerTV2 = (TextView) view.findViewById(R.id.tv_home_scroll_text2);
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                isShow = !isShow;
                if (position == list.size()) {
                    position = 0;
                }

                // 之下而上出现
                startY2 = isShow ? offsetY : 0;
                endY2 = isShow ? 0 : -offsetY;
                ObjectAnimator
                        .ofFloat(mBannerTV1, "translationX", startY2, endY2)
                        .setDuration(4000).start();

                // 之下而上消失
                startY1 = isShow ? 0 : offsetY;
                endY1 = isShow ? -offsetY : 0;
                ObjectAnimator
                        .ofFloat(mBannerTV2, "translationX", startY1, endY1)
                        .setDuration(4000).start();

                if (list.size() - 1 >= position) {//保证数组不越界
                    if (isShow) {
                        mBannerTV1.setText(list.get(position));
                    } else {
                        mBannerTV2.setText(list.get(position));
                    }
                }
                position++;
//				//position持续自增
//				//切换城市时如果某城市 notice.size < position则滚动内容会卡成2条重复的
//				if(position >= list.size()){
//					position = 0;
//				}
                handler.postDelayed(runnable, 5000);
            }
        };
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(list);
        } else {
            this.list = list;
        }
    }

    public void startScroll() {
        handler.post(runnable);
    }

    public void stopScroll() {
        handler.removeCallbacks(runnable);
        position = 0;
    }
}
